package com.zzl.study.auth.security.authentication.context;

import com.zzl.study.auth.utils.JacksonUtils;
import lombok.Data;
import org.springframework.security.oauth2.core.oidc.OidcIdToken;

import java.net.URL;
import java.time.Instant;
import java.util.List;

/**
 * @author: zhangzl
 * @date: 2024/6/6 14:35
 * @version: 1.0
 * @description: 自定义ID_TOKEN
 */
@Data
public class IdToken {

    private String tokenValue;

    private Instant issuedAt;

    private Instant expiresAt;

    private Claims claims;

    private URL issuer;

    private String authorizationCodeHash;

    private List<String> authenticationMethods;

    private String authenticationContextClass;

    private String nonce;

    private List<String> audience;

    private String accessTokenHash;

    private String authorizedParty;

    private Instant authenticatedAt;

    private String subject;

    private Address address;

    private String locale;

    private String fullName;

    private String zoneInfo;

    private String profile;

    private String preferredUsername;

    private Boolean phoneNumberVerified;

    private String familyName;

    private String nickName;

    private Instant updatedAt;

    private String picture;

    private Boolean emailVerified;

    private String website;

    private String email;

    private String birthdate;

    private String givenName;

    private String middleName;

    private String gender;

    private String phoneNumber;

    public static OidcIdToken to(IdToken idToken) {
        if (idToken == null) {
            return null;
        }
        return new OidcIdToken(idToken.getTokenValue(), idToken.getIssuedAt(), idToken.getExpiresAt(), JacksonUtils.toMap(idToken.getClaims()));
    }

    public static IdToken of(OidcIdToken oidcIdToken) {
        if (oidcIdToken == null) {
            return null;
        }
        IdToken idToken = new IdToken();
        idToken.setTokenValue(oidcIdToken.getTokenValue());
        idToken.setIssuedAt(oidcIdToken.getIssuedAt());
        idToken.setExpiresAt(oidcIdToken.getExpiresAt());
        idToken.setClaims(JacksonUtils.toObject(oidcIdToken.getClaims(), Claims.class));
        idToken.setIssuer(oidcIdToken.getIssuer());
        idToken.setAuthorizationCodeHash(oidcIdToken.getAuthorizationCodeHash());
        idToken.setAuthenticationMethods(oidcIdToken.getAuthenticationMethods());
        idToken.setAuthenticationContextClass(oidcIdToken.getAuthenticationContextClass());
        idToken.setNonce(oidcIdToken.getNonce());
        idToken.setAudience(oidcIdToken.getAudience());
        idToken.setAccessTokenHash(oidcIdToken.getAccessTokenHash());
        idToken.setAuthorizedParty(oidcIdToken.getAuthorizedParty());
        idToken.setAuthenticatedAt(oidcIdToken.getAuthenticatedAt());
        idToken.setSubject(oidcIdToken.getSubject());
        idToken.setAddress(Address.of(oidcIdToken.getAddress()));
        idToken.setLocale(oidcIdToken.getLocale());
        idToken.setFullName(oidcIdToken.getFullName());
        idToken.setProfile(oidcIdToken.getProfile());
        idToken.setPreferredUsername(oidcIdToken.getPreferredUsername());
        idToken.setPhoneNumberVerified(oidcIdToken.getPhoneNumberVerified());
        idToken.setFamilyName(oidcIdToken.getFamilyName());
        idToken.setNickName(oidcIdToken.getNickName());
        idToken.setUpdatedAt(oidcIdToken.getUpdatedAt());
        idToken.setPicture(oidcIdToken.getPicture());
        idToken.setEmailVerified(oidcIdToken.getEmailVerified());
        idToken.setWebsite(oidcIdToken.getWebsite());
        idToken.setEmail(oidcIdToken.getEmail());
        idToken.setBirthdate(oidcIdToken.getBirthdate());
        idToken.setGivenName(oidcIdToken.getGivenName());
        idToken.setMiddleName(oidcIdToken.getMiddleName());
        idToken.setGender(oidcIdToken.getGender());
        idToken.setPhoneNumber(oidcIdToken.getPhoneNumber());
        return idToken;
    }

}
